package com.xjh.utils.response;

import com.alibaba.fastjson.JSON;
import com.xjh.utils.result.Result;
import org.springframework.http.MediaType;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class ResponseWrite {
    /**
     * 响应数据到页面
     *
     * @param response,responseMap
     * @return void
     * @author peijiang.Huang
     * @date 2021-11-15
     */
    public static void write(HttpServletResponse response, Object responseMap, String responseMsg) throws IOException, ServletException {
        response.setContentType(MediaType.APPLICATION_JSON_VALUE);
        response.setCharacterEncoding("UTF-8");
        //response.setStatus(HttpServletResponse.SC_OK);
        PrintWriter writer = response.getWriter();
        writer.write(JSON.toJSONString(Result.fail(responseMsg, responseMap)));
        writer.flush();
        writer.close();
    }

    public static void close(HttpServletResponse response, Object responseMap, String responseMsg) throws IOException {
        response.setContentType(MediaType.TEXT_HTML_VALUE);
        response.setCharacterEncoding("UTF-8");
        //response.setStatus(HttpServletResponse.SC_OK);
        PrintWriter writer = response.getWriter();
        StringBuilder sb = new StringBuilder();
        sb.append("<script>\n" +
                "alert('"+responseMsg+"');\n" +
                "window.onload=function(){\n" +
                "\tCloseWebPage()\n" +
                "}\n" +
                "function CloseWebPage(){\n" +
                "\tif(navigator.userAgent.indexOf(\"MSIE\")>0)\n" +
                "\t{\n" +
                "\t\tif(navigator.userAgent.indexOf(\"MSIE6.0\")>0)\n" +
                "\t\t{\n" +
                "\t\t\twindow.opener=null;window.close();\n" +
                "\t\t}\n" +
                "\t\telse{\n" +
                "\t\t\twindow.open('','_top');window.top.close();\n" +
                "\t\t}\n" +
                "\t}\n" +
                "\telse if(navigator.userAgent.indexOf(\"Firefox\")>0){\n" +
                "\t\twindow.location.href='about:blank';\n" +
                "\t}else{\n" +
                "\t\twindow.opener=null;\n" +
                "\t\twindow.open('','_self','');\n" +
                "\t\twindow.close();\n" +
                "\t}\n" +
                "}\n" +
                "</script>");
        //writer.write("<script>alert('" + responseMsg + "');window.onload=function(){CloseWebPage()}function CloseWebPage(){if(navigator.userAgent.indexOf(\"MSIE\")>0){if(navigator.userAgent.indexOf(\"MSIE 6.0\")>0){window.opener=null;window.close();}else{window.open('','_top');window.top.close();}}else if(navigator.userAgent.indexOf(\"Firefox\")>0){window.location.href='about:blank ';}else{window.opener=null;window.open('','_self','');window.close();}}</script>");
        writer.write(sb.toString());
        writer.flush();
        writer.close();
    }

    /**
     * 响应数据到页面
     *
     * @param response,responseMap
     * @return void
     * @author peijiang.Huang
     * @date 2021-11-15
     */
    public static void write(HttpServletResponse response, String responseMsg) throws IOException {
        response.setContentType(MediaType.APPLICATION_JSON_VALUE);
        response.setCharacterEncoding("UTF-8");
        PrintWriter writer = response.getWriter();
        writer.write(responseMsg);
        writer.flush();
        writer.close();
    }

}
